package com.jsy.common.util.secret;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;

public class WizbankSecret {
    private static final String ProviderSunJCE = "com.sun.crypto.provider.SunJCE";
    private static final String ProviderSUN = "sun.security.provider.Sun";
    public static String encryptPwd(String str, String encryptionKey){
        Key key = null;
        Cipher cipher = null;
        //Generate a DES KEY
        try {
            Security.addProvider((Provider)Class.forName(ProviderSunJCE).newInstance());
            Security.addProvider((Provider)Class.forName(ProviderSUN).newInstance());

            KeyGenerator generator = KeyGenerator.getInstance("DES");
            SecureRandom srn = SecureRandom.getInstance("SHA1PRNG", "SUN");
            srn.setSeed(encryptionKey.getBytes());
            generator.init(srn);
            key = generator.generateKey();
            byte[] iv = new byte[8];
            srn.nextBytes(iv);
            IvParameterSpec params = new IvParameterSpec(iv, 0, 8);
            //Generates a Cipher object that implements DES transformation
            cipher = Cipher.getInstance("DES/OFB16/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE,key, params);
        } catch (Exception e) {
            e.printStackTrace();
        }

        byte[] stringBytes = str.getBytes();
        byte[] raw = new byte[0];
        try {
            raw = cipher.doFinal(stringBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return new String(encoder.encode(raw));
    }

    public static String decrypt(String str, String encryptionKey) {
        try {
            Key key = null;
            Cipher cipher = null;

            //Generate a DES KEY
            Security.addProvider(
                    (Provider) Class.forName(ProviderSunJCE).newInstance());
            Security.addProvider(
                    (Provider) Class.forName(ProviderSUN).newInstance());
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            SecureRandom srn = SecureRandom.getInstance("SHA1PRNG", "SUN");
            srn.setSeed(encryptionKey.getBytes());
            generator.init(srn);
            key = generator.generateKey();

            byte[] iv = new byte[8];
            srn.nextBytes(iv);
            IvParameterSpec params = new IvParameterSpec(iv, 0, 8);
            cipher = Cipher.getInstance("DES/OFB16/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, key, params);
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] raw = cipher.doFinal(decoder.decodeBuffer(str));
            String result = new String(raw, "ISO-8859-1");
            //return the decrypted password
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) {

//        String pwd = "admin";
//        String encryptionKey = "123456";
        String pwd = "X770+OsWd2Xrn7Pb";
        String encryptionKey = "admin";
        String key = new StringBuffer(encryptionKey).reverse().toString();
//        String encryptPwd = WizbankSecret.encryptPwd(pwd, key);
//        System.out.println(encryptPwd);
        String encryptPwd = "XaX1wqsUdWLq8ck=";
        String decrypt = WizbankSecret.decrypt(encryptPwd, key);
        System.out.println(decrypt);
    }
}
